package comm.suger.web.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
 * @ClassName: CookieUtils
 * @Description: Cookie操作工具类
 * @author Lanxiaowei
 * @date 2014-2-23 下午5:37:13
 */
public class CookieUtils {
	/**
	 * @Title: getHttpOnlyCookie
	 * @Description: 获取HttpOnly Cookie值
	 * @param @param target
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getHttpOnlyCookie(String target) {
		URL url = null;
		String cookie = null;
		try {
			url = new URL(target);
			URLConnection connection = url.openConnection();
			connection.connect();
			String headerName = null;
			for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
				if (headerName.equals("Set-Cookie") || headerName.equals("Set-Cookie2")) {
					cookie = connection.getHeaderField(i).replace("path=/; HttpOnly", "").trim();
					break;
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cookie;
	}
}
